import {ERR_OK,ERR_NO_CONTENT} from 'config/httpCodeConfig';
import {getSelectType} from 'common/configFunc';

export const CommonFunc = {
	data() {
		return {
			loading: false,
            selectId: '',
            query: '',
            type: '',
            contentList: [],
            titleList: {},
            checkbox: [],
            meta: {}
		}
	},
    created() {
        this.typeList = getSelectType(this.url);
        this.select(); 
    },
	methods: {
		tableAction(type, data) {
            if(type === 'changeStatus') {
                this.update(data.id, {status: Number(data.status)});
            } else if(type === 'edit') {
                this.$router.push(this.url + 's/' + data.id + '/edit')
            } else if(type === 'delete') {
                this.delete(data.id);
            } else if(type === 'checkbox') {
                this.replaceCheckBox(data);
            }
        },
        handleAction(type) {
            if(type === 'delete'){
                this.delete();
            } else if(type === 'create') {
                this.$router.push(this.url + 's/create');
            }
        },
		select(params = {}) {
            this.reverseLoading();
			this.$http.get(this.url, {params: this.buildParams(params)}).then((res) => {
                this.reverseLoading();
	            if (res.status === ERR_OK) {
	                this.titleList = res.data.title;
	                this.contentList = res.data.data;
	                this.meta = res.data.meta;
	                if(this.meta.page === 1) {
	                	this.$message.success('数据获取成功');
	                }
	            } else {
	                this.$message.error('服务器挂了');
	            }
	        })
		},
		update(id, data) {
            this.reverseLoading();
			this.$http.patch(this.url + '/' + id ,data).then((res) => {
                this.reverseLoading();
                if (res.status === ERR_NO_CONTENT){
                    this.$message.success('操作成功');
                } else {
                    this.$message.error('操作失败');
                }
            })
		},
		delete(data = -1) {
			if(data === -1) {
				if(this.checkbox.length === 0) {
					this.$message.info('请选择需删除的记录');
					return false;
				}
				data = this.checkbox;
			}
			this.$confirm('此操作将永久删除这些记录, 是否继续?', '提示', {
            	confirmButtonText: '确定',
            	cancelButtonText: '取消',
            	type: 'warning',
            	center: true
            }).then(() => {
            	this.reverseLoading();
              	this.$http.delete(this.url + '/' + data).then((res)=>{
            		this.reverseLoading()
              		if (res.status === ERR_NO_CONTENT){
	                    this.$message.success('操作成功')
                        this.select()
	                } else {
	                    this.$message.error('操作失败')
	                }
              })
            }).catch(() => {
            	this.$message.info('已取消删除')
            });
		},
		reverseLoading(){
			this.loading = !this.loading;
		},
		replaceCheckBox(data) {
            this.checkbox = [];
            if(!data.length){
                this.checkbox = [];
            }else{
                for(let i=0; i<data.length; i++){
                    this.checkbox.push(data[i].id);
                }
            }
        },
        buildParams(params) {
        	return Object.assign({page: this.page}, params);
        },
        deleteChecked() {
            this.delete();
        },
        selectChange(string) {
            this.selectId = string;
        },
        selectPage(page){
            this.select({'query': this.query, 'page': page});
        },
        search(query,type) {
            this.querySet(query, type);
            let _this = this;
            setTimeout(function() {
                _this.select({'query': _this.query});
            }, 20);
        },
        querySet(string, type) {
            if (type != '') {
                this.query = {};
                this.query[type] = string;
            } else {
                this.query = string;
            }
        }
	}
}